using System.Collections.Generic;
using UnityEngine;

// 通用背包类，支持任意数据类型和对应视图
public class Bag : MonoBehaviour
{
    [Header("BagContent 引用")]
    [SerializeField] private BagContent bagContent; // 必须拖入 Inspector

    // 数据列表（object 类型，兼容所有类型）
    private List<BagItemData> _items = new List<BagItemData>();
    private List<List<BagItemData>> _TwoDitems = new List<List<BagItemData>>();
    public List<BagItemData> Items => _items;

    private List<List<BagItemData>> TwoDitems => _TwoDitems;

    // 当前选中的视图
    private IBagItemView _selectedView;
    public IBagItemView SelectedView
    {
        get => _selectedView;
        set
        {
            _selectedView = value;
            if (bagContent == null) return;

            // 取消所有高亮
            foreach (var view in bagContent.GetViews())
                view.IsHighlighted = false;

            // 设置选中高亮
            if (_selectedView != null)
                _selectedView.IsHighlighted = true;
        }
    }

    /// <summary>
    /// 初始化背包数据
    /// </summary>
    /// <param name="items">数据列表</param>
    public void Setup(List<BagItemData> items)
    {
        _items = items;
        bagContent.Setup(items);
    }
     public void Setup(List<List<BagItemData>> TwoDitems)
    {
        _TwoDitems = TwoDitems;
        bagContent.Setup(TwoDitems);
    }

    /// <summary>
    /// 刷新所有视图
    /// </summary>
    public void Refresh()
    {
        if (bagContent != null)
            bagContent.Refresh();
    }

    /// <summary>
    /// 获取当前视图列表
    /// </summary>
    /// <returns>IBagItemView 列表</returns>
    public List<IBagItemView> GetViews()
    {
        if (bagContent != null)
            return bagContent.GetViews();
        return new List<IBagItemView>();
    }

    /// <summary>
    /// 移动视图
    /// </summary>

    /// <summary>
    /// 高亮指定索引的视图
    /// </summary>
    /// <param name="index"></param>
    public void HighlightIndex(int index)
    {
        var views = GetViews();
        if (index < 0 || index >= views.Count) return;

        SelectedView = views[index];
    }
}
